<?php

/**
 * Created by PhpStorm.
 * User: leo
 * Date: 2017/8/25
 * Time: 16:31
 */
namespace common;
use common\models\OrderInfo;
use common\models\Pay;
use DateTime;

class OrderProcess
{
    public static function getOrderInfo($park_current_id)
    {
        $park_current = \common\models\ParkCurrent::findOne($park_current_id);
        $order  = OrderInfo::findOne(['park_current_id'=>$park_current_id]);
        $spaces = \common\models\ParkCurrentSpace::findAll(['park_current_id'=>$park_current_id]);
        $quantity =  count($spaces);
        $datetimeStart = new DateTime($park_current->start_time) ;
        $datetimeNow = new DateTime() ;
        $interval = $datetimeNow->diff($datetimeStart);

        if($order)
            $order->delete();

        $orderid = str_replace("-","", \thamtech\uuid\helpers\UuidHelper::uuid());
        $order = new OrderInfo([
            'id'=>$orderid,
            'park_current_id'=>$park_current_id,
            'start_time'=> $park_current->start_time,
            'end_time'=> $datetimeNow->format('Y-m-d H:i:s'),
            'space_num'=> $quantity,
            'spaces'=> $datetimeNow->format('Y-m-d H:i:s'),
            'fee_type'=> $datetimeNow->format('Y-m-d H:i:s'),
            'fee_parms'=> $datetimeNow->format('Y-m-d H:i:s'),
            'fee_origin'=> $datetimeNow->format('Y-m-d H:i:s'),
            'fee_discount'=> '0',
            'fee_money'=> '1',
            'status'=>0
        ]);
        $order->save();



        return [
            'quantity'=>$quantity,
            'order_id'=>$order->id,
            'from'=>$datetimeStart->format('Y-m-d H:i'),
            'to'=>$datetimeNow->format('Y-m-d H:i'),
            'duration'=>$interval->format('%d天%H小时%i分%s秒'),
            'fee'=>$order->fee_money,
        ];
    }


    public static function setOrderStatus($order,$status)
    { 
        if (!$order) { // 如果订单不存在
            return 'Order not exist.'; // 告诉微信，我已经处理完了，订单没找到，别再通知我了
        } 
        if ($order->status == 1) { // 假设订单字段“支付时间”不为空代表已经支付
            return true; // 已经支付成功了就不再更新了
        } 
        $order->status = 1;
        $order->save();
        return true; // 返回处理完成
    }

}